/******************************************************************************/
#include "stdafx.h"
#include "Main.h"
/******************************************************************************/
InventoryGui InvGui;
/******************************************************************************/
// MANAGE
/******************************************************************************/
STRUCT(InventoryWindow , Window)
//{
   virtual void update(C GuiPC &gpc)
   {
      super::update(gpc);
      
      if(Gui.ms()==this && Ms.bp(1))
      {
         Ms.eat(1);
         hide();
      }
   }
};
void InventoryGui::create()
{
   gobjs.replaceWindow<InventoryWindow>(); gobjs.load("gui/inventory.gobj"); gobjs.hide(); Gui+=gobjs;

   Window &window=gobjs.getWindow(S);
   window.move(Vec2(D.w()-0.05f-window.rect.max.x, 0));

   window+=slot_image[SLOT_HEAD    ].create((slot_region[SLOT_HEAD    ]=&gobjs.getRegion("head"    ))->rect);
   window+=slot_image[SLOT_NECK    ].create((slot_region[SLOT_NECK    ]=&gobjs.getRegion("neck"    ))->rect);
   window+=slot_image[SLOT_ARM_L   ].create((slot_region[SLOT_ARM_L   ]=&gobjs.getRegion("arm l"   ))->rect);
   window+=slot_image[SLOT_ARM_R   ].create((slot_region[SLOT_ARM_R   ]=&gobjs.getRegion("arm r"   ))->rect);
   window+=slot_image[SLOT_BODY    ].create((slot_region[SLOT_BODY    ]=&gobjs.getRegion("body"    ))->rect);
   window+=slot_image[SLOT_HANDS   ].create((slot_region[SLOT_HANDS   ]=&gobjs.getRegion("hands"   ))->rect);
   window+=slot_image[SLOT_LEGS    ].create((slot_region[SLOT_LEGS    ]=&gobjs.getRegion("legs"    ))->rect);
   window+=slot_image[SLOT_FEET    ].create((slot_region[SLOT_FEET    ]=&gobjs.getRegion("feet"    ))->rect);
   window+=slot_image[SLOT_FINGER_L].create((slot_region[SLOT_FINGER_L]=&gobjs.getRegion("finger l"))->rect);
   window+=slot_image[SLOT_FINGER_R].create((slot_region[SLOT_FINGER_R]=&gobjs.getRegion("finger r"))->rect);

   REPAO(slot_image).rect_color.zero();

   // list
   {
      ListColumn lc[]=
      {
         ListColumn(MEMBER(Item, icon ), 0.21f, L"+"    ), // 0
         ListColumn(MEMBER(Item, name ), 0.52f, L"Name" ), // 1
         ListColumn(MEMBER(Item, power), 0.21f, L"Power"), // 2
      };
      gobjs.getRegion("Backpack")+=list.create(lc, Elms(lc));
      list.column(0).button.text_align=0;
      list.column(2).precision=1; // set showing only 1 decimal precision for power float attribute

      list. cur_mode=LCM_MOUSE;
      list.draw_mode=LDM_RECTS;
      list.flag|=LIST_SCALABLE;
      list.elmHeight(0.08f);
      list.zoom_max*=1.44f ;
   }
}
/******************************************************************************/
// GET
/******************************************************************************/
Bool InventoryGui::visible()
{
   if(Window *window=gobjs.findWindow(S))return window->visible();
   return false;
}
/******************************************************************************/
// OPERATIONS
/******************************************************************************/
void InventoryGui::link(Inventory *inv)
{
   if(T.inv!=inv)
   {
      T.inv=inv;
      setGui();
   }
}
/******************************************************************************/
void InventoryGui::setGui()
{
   if(inv)
   {
      // set item list
      {
         // here we have to hide the items which are assigned to slots
         // create a 'is' array which determines visibility of an element (1=visible, 0=hidden)
         Mems<Bool> visible; visible.setNum(inv->items.elms()); // allocate array of item visibility
         REPAO(visible)=true; // set all visibility by default to true
         REPA(inv->slots) // iterate through all slots
            if(inv->valid(i)) // if the slot is valid
               visible[inv->slots[i]]=false; // set visibility for item assigned to a slot to false, to be hidden on the list
         list.setData(inv->items, visible.data()); // set list data from items container and visibility list
      }

      // set slot images
      REP(SLOT_NUM) // for all slots
         if(i!=SLOT_TEMP) // skip temporary slot because it's not drawn using 'Image' class
            if(slot_region[i])
      {
         Rect     &image_rect=slot_region[i]->rect; // gui rect
         GuiImage &image_item=slot_image [i]      ; // gui image

         if(!inv->valid(i))image_item.set(NULL);else // if there is no item then clear the item image slot
         {
            ImagePtr icon=inv->item(inv->slots[i]).icon; // access item's icon
            image_item.set(icon);                        // set slot image as the item's icon
            if(icon)                                     // set proper scaling
            {
               Vec2 size(icon->x(), icon->y()); size*=PIXEL_SIZE; // set default size
               if(size.x>image_rect.w())size*=image_rect.w()/size.x; // clamp item size to background slot width
               if(size.y>image_rect.h())size*=image_rect.h()/size.y; // clamp item size to background slot height
               Rect rect(image_rect.center()); rect.extend(size/2);
               image_item.setRect(rect);
            }
         }
      }
   }else
   {
      list.clear();
      REPAO(slot_image).set(NULL);
   }
}
/******************************************************************************/
void InventoryGui::toggle()
{
   if(visible())hide();else show();
}
void InventoryGui::show()
{
   gobjs.getWindow(S).show();
}
void InventoryGui::hide()
{
   gobjs.getWindow(S).hide();
}
/******************************************************************************/
// UPDATE
/******************************************************************************/
void InventoryGui::update(Player &player)
{
   if(inv)
   {
      if(Ms.bp(0)) // if mouse button pressed
      {
         if(InRange(inv->slots[SLOT_TEMP], inv->items)) // if we have an item attached with mouse
         {
            if(Gui.ms()==&list) // if mouse cursor is on the list
            {
               inv->slots[SLOT_TEMP]=-1; // clear the slot reference which will result in "putting back the item into the list"
               setGui();                 // update visuals
               player.sendInvSlots();
            }
         }
         else // we don't have an item so we want to get one
         {
            if(Gui.ms()==&list) // from the list
            {
               if(list())
               {
                  inv->slots[SLOT_TEMP]=list.visToAbs(list.cur);
                  setGui(); // update visuals
                  player.sendInvSlots();
               }
            }
         }

         REPA(slot_image)
            if(Gui.ms()==&slot_image[i] || Gui.ms()==slot_region[i]) // if we want to swap temp item with i-th slot
               if(inv->slotsCanBeSwapped(SLOT_TEMP, SLOT_TYPE(i)))
         {
            Swap(inv->slots[SLOT_TEMP], inv->slots[i]); // swap temporary with i-th slot
            setGui();                                   // update visuals
            player.sendInvSlots();
         }
      }
   }
}
/******************************************************************************/
// DRAW
/******************************************************************************/
void InventoryGui::draw()
{
   if(inv && InRange(inv->slots[SLOT_TEMP], inv->items))
   {
   	Vec2 pos=Ms.pos();
   	inv->item(inv->slots[SLOT_TEMP]).drawIcon(pos);
   }
}
/******************************************************************************/
